//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmSimpleSwingCondition.h"
using namespace Cephei::QL::Methods::Finitedifferences::Stepconditions;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Methods/Finitedifferences/Meshers/FdmMesher.h>
#include <gen/QL/Methods/Finitedifferences/Utilities/FdmInnerValueCalculator.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
using namespace Cephei::QL::Methods::Finitedifferences::Utilities;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::CFdmSimpleSwingCondition (Cephei::Core::IVector<Double>^ exerciseTimes, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmInnerValueCalculator^ calculator, UInt64 swingDirection) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CFdmMesher^ _Cmesher;
    CFdmInnerValueCalculator^ _Ccalculator;
    try
    {
#ifdef HANDLE
        _phFdmSimpleSwingCondition = NULL;
#endif
        exerciseTimes->Lock();
        INativeVector<Double>^ _NCIexerciseTimes = exerciseTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCexerciseTimes = safe_cast<CDoubleVector^>(_NCIexerciseTimes);
        std::vector<QuantLib::Time>& _exerciseTimes = static_cast<std::vector<QuantLib::Time>&> (_NCexerciseTimes->GetReference ());
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _Ccalculator = safe_cast<CFdmInnerValueCalculator^> (calculator);
        _Ccalculator->Lock();
        boost::shared_ptr<QuantLib::FdmInnerValueCalculator>& _calculator = static_cast<boost::shared_ptr<QuantLib::FdmInnerValueCalculator>&> (_Ccalculator->GetShared ()); 
        QuantLib::Size _swingDirection = (QuantLib::Size)ValueHelper::Convert (swingDirection); //d
        _ppFdmSimpleSwingCondition = new boost::shared_ptr<QuantLib::FdmSimpleSwingCondition> (new QuantLib::FdmSimpleSwingCondition ( _exerciseTimes,  _mesher,  _calculator,  _swingDirection ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (exerciseTimes != nullptr) exerciseTimes->Unlock();    //not optional
        if (_Cmesher != nullptr) _Cmesher->Unlock();
        if (_Ccalculator != nullptr) _Ccalculator->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::CFdmSimpleSwingCondition (boost::shared_ptr<QuantLib::FdmSimpleSwingCondition>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSimpleSwingCondition = NULL;
#endif
	_ppFdmSimpleSwingCondition = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::CFdmSimpleSwingCondition (QuantLib::FdmSimpleSwingCondition& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmSimpleSwingCondition = NULL;
#endif
	_ppFdmSimpleSwingCondition = new boost::shared_ptr<QuantLib::FdmSimpleSwingCondition> (&childNative);
    
    _FdmSimpleSwingConditionOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::CFdmSimpleSwingCondition (CFdmSimpleSwingCondition^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSimpleSwingCondition = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmSimpleSwingCondition = new boost::shared_ptr<QuantLib::FdmSimpleSwingCondition> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::CFdmSimpleSwingCondition (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSimpleSwingCondition = NULL;
#endif
	if (!t->IsSubclassOf(CFdmSimpleSwingCondition::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::CFdmSimpleSwingCondition (QuantLib::Handle<QuantLib::FdmSimpleSwingCondition>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmSimpleSwingCondition = &childNative;
	_ppFdmSimpleSwingCondition = &static_cast<boost::shared_ptr<QuantLib::FdmSimpleSwingCondition>>(childNative.currentLink());
    
    _FdmSimpleSwingConditionOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::CFdmSimpleSwingCondition (QuantLib::Handle<QuantLib::FdmSimpleSwingCondition> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmSimpleSwingCondition = &childNative;
	_ppFdmSimpleSwingCondition = &static_cast<boost::shared_ptr<QuantLib::FdmSimpleSwingCondition>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::CFdmSimpleSwingCondition (QuantLib::FdmSimpleSwingCondition childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSimpleSwingCondition = NULL;
#endif
	_ppFdmSimpleSwingCondition = new boost::shared_ptr<QuantLib::FdmSimpleSwingCondition> (new QuantLib::FdmSimpleSwingCondition (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::~CFdmSimpleSwingCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmSimpleSwingCondition != NULL)
    {
	    delete _ppFdmSimpleSwingCondition;
        _ppFdmSimpleSwingCondition = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::!CFdmSimpleSwingCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmSimpleSwingCondition != NULL)
    {
	    delete _ppFdmSimpleSwingCondition;
    }
}
QuantLib::FdmSimpleSwingCondition& Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::GetReference ()
{
    if (_ppFdmSimpleSwingCondition == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmSimpleSwingCondition;
}
boost::shared_ptr<QuantLib::FdmSimpleSwingCondition>& Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::GetShared ()
{
    if (_ppFdmSimpleSwingCondition == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmSimpleSwingCondition;
}
QuantLib::FdmSimpleSwingCondition* Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::GetPointer ()
{
    if (_ppFdmSimpleSwingCondition == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmSimpleSwingCondition;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmSimpleSwingCondition>& Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::GetHandle ()
{
	if (_phFdmSimpleSwingCondition == NULL)
	{
		_phFdmSimpleSwingCondition = new Handle<QuantLib::FdmSimpleSwingCondition> (*_ppFdmSimpleSwingCondition);
	}
	return *_phFdmSimpleSwingCondition;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::HasNative () 
{
	return (_ppFdmSimpleSwingCondition != NULL);
}

Cephei::QL::Methods::Finitedifferences::Stepconditions::IFdmSimpleSwingCondition^ Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition::ApplyTo (Cephei::QL::Math::IArray^ a, Double t)
{
    CArray^ _Ca;
    try
    {
        _Ca = safe_cast<CArray^> (a);
        _Ca->Lock();
        QuantLib::Array& _a = static_cast<QuantLib::Array&> (_Ca->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
    	(*_ppFdmSimpleSwingCondition)->applyTo ( _a,  _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ca != nullptr) _Ca->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Stepconditions::IFdmSimpleSwingCondition^ Cephei::QL::Methods::Finitedifferences::Stepconditions::CFdmSimpleSwingCondition_Factory::Create (Cephei::Core::IVector<Double>^ exerciseTimes, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmInnerValueCalculator^ calculator, UInt64 swingDirection)
{
    return REFNEW CFdmSimpleSwingCondition ( exerciseTimes,  mesher,  calculator,  swingDirection);
}
